package websimulator;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import buildings.House;
import buildings.LinkedHouse;
import buildings.School;
import buildings.Supermarket;
import buildings.TerrachedHouse;

import com.sun.jndi.url.corbaname.corbanameURLContextFactory;

import calculations.GraphModul;
import calculations.LevelOfIllumination;
import calculations.Moduls;
import calculations.Temperatur;


import readwrite.Connector;

/**
 * Servlet implementation class BigSimulation
 */
@WebServlet("/BigSimulation")
public class BigSimulation extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArrayList<Moduls> modul;
    private Connector con;  
    private Temperatur temp = new Temperatur();
	private LevelOfIllumination levelOfIllumination = new LevelOfIllumination();
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public BigSimulation() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		 
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int simId = Integer.parseInt(request.getParameter("simId"));
		ArrayList<Moduls> moduls;
		ArrayList<House> modulHouses;
		ArrayList<House> houses = new ArrayList<House>();
		try {
			moduls = con.getBigSimulationModuls(simId);
			for(Moduls m: moduls)
			{
				modulHouses = con.getHousesByModulID(m.getId());
				for(House h: modulHouses)
				{
					
					houses.add(h);
				}
			}
				
			for (int i = 0; i < 24; i++)
			{
				temp.addTemperaturInside (i, request.getParameter ("t" + i));			
				temp.addTemperaturOutside (i, request.getParameter ("to" + i));
				levelOfIllumination.addLevel (i, request.getParameter ("i" + i));						
			}
			
			OutputStream out = response.getOutputStream();
			GraphModul graph = new GraphModul(houses, temp, levelOfIllumination);
			JFreeChart chart = graph.createChart();
		
			ChartUtilities.writeChartAsPNG(out, chart, 800, 800);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
